﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Skynet.WebFoundation.Services.Navigation;
using Skynet.WebFoundation.Services.Session;
using System.Web.UI.WebControls;
using Skynet.BusinessEntities;
using System.ComponentModel;
using System.Xml.Linq;
using Infrastructure.Aspect.Dynamic;

namespace Skynet.WebAdministracion.Views.DesignExamViewer
{
    [System.ComponentModel.DataObject]
    public class DesignExamViewerPresenter : Infrastructure.Web.Views.Master.MasterPresenter<IDesignExamViewerView>
    {
        [Dependency]
        public BusinessLogic.BusinessComponents.IBCExamQueries BCExamQueries { get; set; }

        [Dependency]
        public BusinessLogic.BusinessComponents.IBCExams BCExams { get; set; }

        [Dependency]
        public INavigationService NavigationService { get; set; }

        

        [Dependency]
        public ISessionService SessionService { get; set; }

        public override void OnViewInitialized()
        {
            var exam = this.BCExamQueries.SearchExams(options: BusinessLogic.BusinessComponents.ExamSearchOptions.GetOne, id: this.SessionService.DesignExamId).SingleOrDefault();
            this.View.ExamTitle = exam.Title;
            BusinessEntities.BaseQuestion question = null;
            if (this.SessionService.DesignQuestionId != null)
            {
                question = this.BCExamQueries.SearchQuestion(options: BusinessLogic.BusinessComponents.QuestionSearchOptions.DesignOneWithAlternatives, QuestionId: this.SessionService.DesignQuestionId).Single();                
            }
            else
            {
                this.SessionService.DesignQuestionId = Guid.NewGuid();
                var tquestion = new BusinessEntities.TextQuestion();
                tquestion.Id = this.SessionService.DesignQuestionId.Value;
                tquestion.Category = "New";
                tquestion.ExamId = this.SessionService.DesignExamId.Value;
                tquestion.Question = "Empty";
                tquestion.Title = "New";
                tquestion.State = ObjectState.Added;
                this.BCExams.MaintenanceQuestions(options: BusinessLogic.BusinessComponents.MaintenanceQuestionsOptions.CUD, question: tquestion);
                question = tquestion;                
            }
            this.View.QuestionCategory = question.Category;
            this.View.QuestionTitle = question.Title;
            if (question is TextQuestion)
            {
                var target = question as BusinessEntities.TextQuestion;
                this.View.QuestionControl = new WebFoundation.Controls.Design.TextQuestionControl() { Text = target.Question, TextMode = TextBoxMode.MultiLine, Width = Unit.Percentage(100) };
            }
        }
        public override void OnViewLoaded()
        {
            var question = this.BCExamQueries.SearchQuestion(options: BusinessLogic.BusinessComponents.QuestionSearchOptions.DesignOneWithAlternatives, QuestionId: this.SessionService.DesignQuestionId).Single();
            if (question is TextQuestion)
            {                
                var target = question as BusinessEntities.TextQuestion;
                this.View.QuestionControl = new WebFoundation.Controls.Design.TextQuestionControl() { TextMode = TextBoxMode.MultiLine, Width = Unit.Percentage(100) };
            }
        }
        public void ChangeQuestionType()
        {

        }
        [DataObjectMethod(DataObjectMethodType.Select, false)]
        public IEnumerable<object> GetAlternatives()
        {
            var result = this.BCExamQueries.QueryAlternatives(options: BusinessLogic.BusinessComponents.QueryAlternativesOptions.DesignByQuestion, QuestionId: this.SessionService.DesignQuestionId);
            if (string.IsNullOrWhiteSpace(result)) return null;  

            var query = from c in XElement.Parse(result).Elements("Alternative") 
                        select new CustomXmlExpandoObject() { Id = Guid.Parse(c.Element("Id").Value) }.BString  ("Alternative",  c.Element("Alternative"));
            return query.ToList();            
        }

        public void NewAlternative()
        {
            this.UpdateQuestionInformation();
            this.SessionService.DesignAlternativeId = null;
            this.NavigationService.NavigateRoute(Constants.RoutesNames.DesignAlternative);
        }
        public void ShowAlternative(Guid AlternativeId)
        {
            this.UpdateQuestionInformation();
            this.SessionService.DesignAlternativeId = AlternativeId;
            this.NavigationService.NavigateRoute(Constants.RoutesNames.DesignAlternative);
        }
        private void UpdateQuestionInformation()
        {
            var question = this.BCExamQueries.SearchQuestion(options: BusinessLogic.BusinessComponents.QuestionSearchOptions.DesignOneWithAlternatives, QuestionId: this.SessionService.DesignQuestionId).Single();
            question.Category = this.View.QuestionCategory;
            question.Title = this.View.QuestionTitle;
            if (question is TextQuestion)
            {
                var target = question as BusinessEntities.TextQuestion;
                var control =  this.View.QuestionControl as TextBox;
                target.Question = control.Text;
            }
            question.State = ObjectState.Modified;
            this.BCExams.MaintenanceQuestions(options: BusinessLogic.BusinessComponents.MaintenanceQuestionsOptions.CUD, question: question);
        }

        public void NewQuestion()
        {
            UpdateQuestionInformation();
            this.SessionService.DesignQuestionId = null;
            this.NavigationService.NavigateRoute(WebFoundation.Constants.RoutesNames.DesignQuestion);
        }

        public void BackToExams()
        {
            UpdateQuestionInformation();
            this.NavigationService.NavigateRoute(WebFoundation.Constants.RoutesNames.DesignExam);
        }
    }
}
